package com.lvtongka.app.fragment.home.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.bumptech.glide.Glide
import com.lvtongka.app.R
import com.lvtongka.app.databinding.HomeGridItemLayoutBinding
import com.lvtongka.app.fragment.home.bean.OfflineRecord
import com.lvtongka.app.http.common.UrlConstants

class HomeOfflineGridAdapter(val context: Context) : BaseAdapter() {
    private var list: MutableList<OfflineRecord> = mutableListOf()

    fun setOfflineList(listData: MutableList<OfflineRecord>) {
        list.clear()
        list.addAll(listData)
        notifyDataSetChanged()
    }

    override fun getCount(): Int {
        return list.size
    }

    override fun getItem(position: Int): OfflineRecord {
        return list[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var holder: MyGridViewHolder
        var finalView = convertView
        if (finalView == null) {
            var binding =
                HomeGridItemLayoutBinding.inflate(LayoutInflater.from(context), parent, false)
            finalView = binding.root
            holder = MyGridViewHolder(binding)
            finalView.tag = holder
        } else {
            holder = finalView.tag as MyGridViewHolder
        }
        var ic = list[position]
        ic?.let {
            Glide.with(holder.binding.ivMyIcon)
                .load(UrlConstants.BASE_APP + it.logo_img)
                .error(R.mipmap.icon_home_seat)
                .into(holder.binding.ivMyIcon)
            holder.binding.tvMyName.text = it.name.trim()
        }
        return finalView!!
    }

    open inner class MyGridViewHolder(var binding: HomeGridItemLayoutBinding)
}